//
//  RoadDetialViewController.m
//  NineGroupProject
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 lushan. All rights reserved.
//

#import "RoadDetialViewController.h"
#import "UIImageView+WebCache.h"
#import "LSAppTools.h"
#import "RoadDetailModel.h"
#import "RoadDetialTableViewCell.h"
#import "RoadDetailTodayTableViewCell.h"
@interface RoadDetialViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) NSMutableArray *memoArray;
@property (nonatomic, strong) NSMutableArray *planArray;
@property (nonatomic, strong) NSMutableArray *cellArray;
@end

@implementation RoadDetialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImage *leftImage = [UIImage imageNamed:@"iconfont-fanhui-2.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction)];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 66 * HEI) style:UITableViewStylePlain];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    
    self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 220)];
    self.titleView.backgroundColor = [UIColor blueColor];
    self.myTableView.tableHeaderView = self.titleView;
    UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 220)];
    [self.myTableView registerClass:[RoadDetailTodayTableViewCell class] forCellReuseIdentifier:@"RoadDetailTodayTableViewCell"];
    [self.myTableView registerClass:[RoadDetialTableViewCell class] forCellReuseIdentifier:@"RoadDetialTableViewCell"];
    
    [titleImageView sd_setImageWithURL:[NSURL URLWithString:self.roadModel.image_url] placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
    [self.titleView addSubview:titleImageView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 160, WIDTH - 20, 15)];
    titleLabel.text = self.roadModel.name;
    titleLabel.font = [UIFont boldSystemFontOfSize:15];
    titleLabel.textColor = [UIColor whiteColor];
    [self.titleView addSubview:titleLabel];
    
    NSString *count = [NSString stringWithFormat:@"%@天 / %@个旅行地", self.roadModel.plan_days_count, self.roadModel.plan_nodes_count];
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 180, WIDTH - 20, 15)];
    timeLabel.text = count;
    timeLabel.font = [UIFont boldSystemFontOfSize:15];
    timeLabel.textColor = [UIColor whiteColor];
    [self.titleView addSubview:timeLabel];
    

    
    
    self.memoArray = [NSMutableArray array];
    self.planArray = [NSMutableArray array];
    self.cellArray = [NSMutableArray array];
    
    [self getAFNetWorkingData];

}

-(void)getAFNetWorkingData{
     NSString *url = [NSString stringWithFormat:@"http://chanyouji.com/api/plans/%@.json?page=1",self.roadModel.nameId];
    [LSAppTools getDataWithURL:url blcok:^(id result) {
        NSMutableDictionary *bigDic = result;
        NSMutableArray *array = [bigDic objectForKey:@"plan_days"];
        
        for (NSMutableDictionary *dic in array) {
            NSString *memo = [dic objectForKey:@"memo"];
            [self.memoArray addObject:memo];
            NSMutableArray *mArray = [dic objectForKey:@"plan_nodes"];
            NSInteger count = mArray.count;
            NSString *str = [NSString stringWithFormat:@"%ld",count];
            [self.cellArray addObject:str];
            
            for (NSMutableDictionary *mDic in mArray) {
                RoadDetailModel *roadDetailModel = [[RoadDetailModel alloc] init];
                [roadDetailModel setValuesForKeysWithDictionary:mDic];
                [self.planArray addObject:roadDetailModel];
            }
            
        }
        [self.myTableView reloadData];

        
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.memoArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[self.cellArray objectAtIndex:section] integerValue];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RoadDetialTableViewCell *roadDetailCell = [tableView dequeueReusableCellWithIdentifier:@"RoadDetialTableViewCell"];
    RoadDetailTodayTableViewCell *roadDetailTodayCell = [tableView dequeueReusableCellWithIdentifier:@"RoadDetailTodayTableViewCell"];
    roadDetailCell.selectionStyle = UITableViewCellSelectionStyleNone;
    roadDetailTodayCell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    NSInteger sum = 0;
    for (int i = 0; i < indexPath.section; i++) {
        sum +=  [[self.cellArray objectAtIndex:i] integerValue];
    }
    
    if (indexPath.row == 0) {
        roadDetailTodayCell.memo = [self.memoArray objectAtIndex:indexPath.section];
        return roadDetailTodayCell;
    }else{
        roadDetailCell.roadDetailModel = [self.planArray objectAtIndex:(indexPath.row + sum)];
        roadDetailCell.s = [NSString stringWithFormat:@"%ld",indexPath.row];
        return roadDetailCell;
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSInteger sum = 0;
    for (int i = 0; i < section; i++) {
        sum +=  [[self.cellArray objectAtIndex:i] integerValue];
    }
    
    RoadDetailModel *roadDetailModel = [[RoadDetailModel alloc] init];
    if (self.planArray.count > sum +2 || self.planArray.count == 2) {
        roadDetailModel = [self.planArray objectAtIndex:sum + 2];
    }else if(self.planArray.count == 1){
        roadDetailModel = [self.planArray objectAtIndex:sum + 1];
    }else{
        roadDetailModel = [self.planArray objectAtIndex:sum];
    }
    
    NSString *place = [roadDetailModel.destination objectForKey:@"name_zh_cn"];
    NSString *data = [NSString stringWithFormat:@"DAY%ld %@", section + 1, place];
    
    return data;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    RoadDetailModel *roadDetailModel = [[RoadDetailModel alloc] init];
    NSInteger sum = 0;
    for (int i = 0; i < indexPath.section; i++) {
        sum +=  [[self.cellArray objectAtIndex:i] integerValue];
    }
    roadDetailModel = [self.planArray objectAtIndex:sum + indexPath.row];
    CGFloat height;
    
    if (indexPath.row == 0) {
        height = [RoadDetialTableViewCell heightForContent:[self.memoArray objectAtIndex:indexPath.section]];
        return height + 70;
        
    }else{
        height = [RoadDetialTableViewCell heightForContent:roadDetailModel.tips];
        return height + 300;
    }
}



-(void)leftButtonAction{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
